<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>


<script type="text/javascript">
	$(function() {
		parent.$.messager.progress({
			text : '数据加载中....'
		});
		$('#functiontree').tree({
			url : '${ctx}/sys/res/treeGrid',
			checkbox : true,
			idFiled:'id',
			textFiled:'text',
			parentField :'pid',
			onLoadSuccess : function(node,data) {
				$.ajax({
					  url: '${ctx}/sys/res/treegridbyid',
					  async:false,
					  data:{roleid:"${id}"},
					  success: function(data){
						  
						  for (var i = 0; i < data.length; i++) {
								var node = $('#functiontree').tree('find', data[i].id);
								if (node) {
									var isLeaf = $('#functiontree').tree('isLeaf', node.target);
									if (isLeaf) {
										$('#functiontree').tree('check', node.target);
									}
								}
							}
						  parent.$.messager.progress('close');
						  collapseAll();
					  }
				});
				
			}
		});
		
		$("#functionListPanel").panel({
					title :"资源列表",
					tools:[{iconCls:'icon-save',handler:function(){mysubmit();}}]
		});
	});
	
	function mysubmit() {
		var nodes = $('#functiontree').tree('getChecked');
		var node = $('#functiontree').tree('getChecked','indeterminate');
        
		var ids = [];
		for (var i = 0; i < nodes.length; i++) {
			ids.push(nodes[i].id);
		}
		 if(node.length!=0){
			 for(var j=0;j<node.length;j++){
				 ids.push(node[j].id);
			 }
		 }
		parent.$.messager.progress({
			text : '正在提交数据....'
		});
		$.post('${ctx}/sys/role/grant/tree/adup', {
			roleid : $('#rid').val(),
			ids : ids.join(',')
		}, function(result) {
			parent.$.messager.progress('close');
			if(result.success){
				reset();
				$.messager.alert('提示', "授权成功");
			}else{
				$.messager.alert('提示', "授权失败");
			}
		}, 'json');
	}
	
	function expandAll() {
		var node = $('#functiontree').tree('getSelected');
		if (node) {
			$('#functiontree').tree('expandAll', node.target);
		} else {
			$('#functiontree').tree('expandAll');
		}
	}
	
	function collapseAll(){
		var node = $('#functiontree').tree('getSelected');
		if (node) {
			$('#functiontree').tree('collapseAll', node.target);
		} else {
			$('#functiontree').tree('collapseAll');
		}
	}
	
	function selecrAll() {
		var node = $('#functiontree').tree('getRoots');
		for ( var i = 0; i < node.length; i++) {
			var childrenNode =  $('#functiontree').tree('getChildren',node[i].target);
			for ( var j = 0; j < childrenNode.length; j++) {
				$('#functiontree').tree("check",childrenNode[j].target);
			}
	    }
	}
	function reset() {
		$('#functiontree').tree('reload');
	}
 
</script>
<div class="easyui-layout" data-options="fit:true">
	<div data-options="region:'center'" style="padding: 1px;">
		<div class="easyui-panel" title="资源列表" style="padding: 1px;"
			data-options="fit:true,border:false" id="functionListPanel">
			<input type="hidden" name="id" value="${id}" id="rid">
			<div align="left" style="margin:5px auto">
				<a  class="easyui-linkbutton"  onclick="selecrAll();">全选</a> 
				<a class="easyui-linkbutton"  onclick="expandAll();">展开</a>
				<a  class="easyui-linkbutton" onclick="collapseAll()";>折叠</a>
				<a class="easyui-linkbutton" onclick="reset();">重置</a>
				
			</div>
			
			<ul id="functiontree"></ul>
		</div>
	</div>
</div>